package com.teaRestaurant.controller.admin;

import com.github.pagehelper.PageInfo;
import com.teaRestaurant.common.result.Result;
import com.teaRestaurant.domain.DTO.ComboPageQueryDTO;
import com.teaRestaurant.domain.DTO.DishPageQueryDTO;
import com.teaRestaurant.domain.VO.DishPageQueryVO;
import com.teaRestaurant.domain.entity.Combo;
import com.teaRestaurant.domain.entity.Dishes;
import com.teaRestaurant.service.ComboService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/combo")
@Slf4j
@Api(tags = "套餐相关接口")
@CrossOrigin
public class ComboController {

    @Autowired
    private ComboService comboService;

    /**
     * 获取套餐列表
     * 筛选条件：名称，分类，售卖状态
     */
    @GetMapping
    public Result list (ComboPageQueryDTO comboPageQueryDTO) {
        PageInfo<Combo> comboPageInfo = comboService.list(comboPageQueryDTO);
        return Result.success(comboPageInfo);
    }

    /**
     * 查询菜品通过id
     * 如何id为0则查询全部
     */
    @GetMapping("/{id}")
    public Result getById (@PathVariable("id") Long id) {
        List<Combo>  combo =  comboService.getById(id);
        return Result.success(combo);
    }

    /**
     * 新增菜品
     */
    @PostMapping
    public Result insert (@RequestBody Combo combo){
        System.out.println(combo.getComboDetailList());
         comboService.insert(combo);
        return Result.success();
    }

    /**
     * 修改菜品
     * TODO
     */
    @PutMapping
    public Result update (@RequestBody Combo combo) {
        comboService.update(combo);
        return Result.success();
    }

    /**
     * 删除菜品
     */
    @DeleteMapping
    public Result delete (@RequestParam String ids) {
        List<Long> ids2 = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
        comboService.delete(ids2);
        return Result.success();
    }


    /**
     * 修改菜品状态状态
     */
    @PutMapping("/updateStatus/{id}")
    public Result updateStatus (@PathVariable("id") Long id) {
        comboService.updateStatus(id);
        return Result.success();
    }

    /**
     * 查看起售套餐数量
     */
    @GetMapping("/getComboSellCount")
    public Result getComboSellCount (){
        Map count = comboService.getComboSellCount();
        return Result.success(count);
    }

}
